// YM2203.cpp: fm.c interface for YM2203 (Yamaha OPN)

#include "opnseries.hpp"
#include "emu2149.h" // Mitsutaka Okazaki's PSG emulator, here due to MAME annoyance

extern "C" {
#include "fm.h"
};

extern "C" void ym2203_update_request(void *param) {
	printf("YM2203 update request as a timer expires, doing nothing.\n");
}


int YM2203::init(unsigned int clock, unsigned int srate) {
	ssg_callbacks ssgCb;

	// Begin creation of SSG first as we need to do that
	this->ssg = PSG_new(clk,srate);
	if (!this->ssg) return 1;

	// construct the callbacks for fm.c to access. resets and clock inits are done manually by us in this interface
	ssgCb.write = this->writePsg;
	ssgCb.read  = this->readPsg;


	// init the FM part
	this->chip = ym2203_init(NULL,NULL,clock,srate,NULL,NULL,&ssgCb);
	if (!this->chip) return 1;

	// i guess that's all we need to do, return success.
	return 0;
}

// *** PSG interface stuff follows
void YM2203::writePsg(void* blah, unsigned char a, unsigned char v) {
	PSG_writeIO(this->ssg, a, v);
}

int YM2203::readPsg(void* blah, unsigned char a) {
	return PSG_readReg(this->ssg,a);
}

void YM2203::irqHandler(void* blah, int irq) {
	if (!this->irq_target_setup) {
		printf("ym2203 IRQ raised but the line isn't connected to anything!\n");
	} else {

	}

}